import { defineStore, acceptHMRUpdate } from 'pinia'

import {
  WebExportData
} from '../models/web/web-data-analysis'
import { exportDataTotal, addTag, deleteTag, getTaglist, getFormDataTable, markFormItem, deleteFormItem,getClickStatics } from '@/service/web/user'



export const useWebDataAnalysisStore = defineStore('webDataAnaiysis', {
  state: (): IWebDataAnalysis => {
    return {
      tagAddList: [],
      detailNavPrev: {} ,
      exportData: new WebExportData(),
      formDataTable: {}
    }
  },
  getters: {

  },
  actions: {
    setExportDataTotal(data: IWebExportData) {
      this.exportData.total = data.total
      this.exportData.site_id = data.site_id
    },
    setDetailNavPrev(data: any) {
      if (data.isClear) {
        this.detailNavPrev = {};
        return;
      }

      // if(data.site_id) data.time = this.detailNavPrev.time;  // 为什么加在这里？
      this.detailNavPrev = _.cloneDeep(data);
    },
    setFormDataTable(data: IWebFormDataTable) {
      this.formDataTable = {};
      if (_.isArray(data.columesList)) {
        data.columesList.splice(3, 1)
      }
      this.formDataTable = data;
    },
    updateFormDataTable(data: any[]) {
      data = data.filter(item => {
        item['is_export'] = '1';
        return item;
      });

      (this.formDataTable as IWebFormDataTable).siteList = _.merge((this.formDataTable as IWebFormDataTable).siteList, data);
    },
    refreshFormDataTable({ index }: {index: number}) {
      (this.formDataTable as IWebFormDataTable).siteList.splice(index, 1);
    },
    // tag
    setTagAddlist(data: IWebTagAddItem[]) {
      this.tagAddList = data;
    },
    exportDataTotal(params: IWebExportDataTotal) {
      let this_ = this
      exportDataTotal(params, function (data) {
        this_.setExportDataTotal({ site_id: params.site_id, total: data })
      })
    },
    addTag(params: IWebDeleteTag) {
      addTag(params, function (data) {
        if (params.callback) params.callback(data);
      });
    },
    deleteTag(params: IWebDeleteTag) {
      deleteTag(params, function (data) {
        if (params.callback) params.callback(data);
      });
    },

    getTaglist(params: any) {
      let this_ = this
      getTaglist(params, function (data) {
        this_.setTagAddlist(data.data)
        if (params && params.callback) params.callback(data);
      });
    },
    getFormDataTable(params:IWebGetFormDataTable) {
      getFormDataTable(params, (data) => {
        this.setFormDataTable(data)
      })
    },
    markFormItem(params: IWebDeleteFormItem) {
      const index = params.index as number
      delete params.index
      markFormItem(params, (data) => {
        if (data == 1) {
          this.refreshFormDataTable({ index })
        }
      })
    },
    deleteFormItem(params:IWebDeleteFormItem) {
      const index = params.index as number
      delete params.index
      deleteFormItem(params, (data) => {
        if (data == 1) {
          this.refreshFormDataTable({ index })
        }
      })
    },
  },
})

if (import.meta.hot) {
  import.meta.hot!.accept(acceptHMRUpdate(useWebDataAnalysisStore, import.meta.hot))
}